package org.aeon.colorfulplanebattle.flying.thread;

import org.aeon.colorfulplanebattle.flying.entity.CollisionPairs;

import java.util.Deque;
import java.util.Iterator;

/**
 * @author: MissingAeon
 * @date: 2024/1/10 10:14
 * @description: 命中管理线程
 */
public class HitThread extends Thread {
    private final Deque<CollisionPairs> hit;
    private boolean flag = true;


    public HitThread(Deque<CollisionPairs> hit) {
        this.setName("命中管理线程!");
        this.hit = hit;
    }

    @Override
    public void run() {
        while (flag) {
            Iterator<CollisionPairs> iterator = hit.iterator();
            while (iterator.hasNext()) {
                CollisionPairs next = iterator.next();
                if (next.getExistenceTime() < 0) {
                    iterator.remove();
                } else {
                    next.setExistenceTime(next.getExistenceTime() - 1);
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
